/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package loganalysis;

/**
 *
 * @author prateekpatil
 */
import java.awt.Color;
import java.io.*;
import java.util.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
//import org.jfree.ui.Spacer;

/**
 * A simple demonstration application showing how to create a line chart using data from an
 * {@link XYDataset}.
 *
 */
public class LineChart extends ApplicationFrame {

    /**
     * Creates a new demo.
     *
     * @param title  the frame title.
     */
    public LineChart(final String title) {

        super(title);
        this.init();
        final XYDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);

    }
    private ArrayList<ArrayList<String>> intermediateData ;
    public final void init ()
    {
        this.ScanData("falkon_summary.txt");
    }
    /**
     * Creates a sample dataset.
     * 
     * @return a sample dataset.
     */
    private XYDataset createDataset() {
        
        final XYSeries series1 = new XYSeries("Num Of Free Workers");
            for (int i =0; i<intermediateData.size();i++)
            {
                series1.add(Double.parseDouble(intermediateData.get(i).get(0)),Double.parseDouble(intermediateData.get(i).get(5)));
            }

        final XYSeries series2 = new XYSeries("Wait Queue Length");
        for (int i =0; i<intermediateData.size();i++)
            {
                series2.add(Double.parseDouble(intermediateData.get(i).get(0)),Double.parseDouble(intermediateData.get(i).get(8)));
            }

        final XYSeries series3 = new XYSeries("Completed Task");
        for (int i =0; i<intermediateData.size();i++)
            {
                series3.add(Double.parseDouble(intermediateData.get(i).get(0)),Double.parseDouble(intermediateData.get(i).get(14)));
            }

        final XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series1);
        dataset.addSeries(series2);
        dataset.addSeries(series3);
                
        return dataset;
        
    }
    
    /**
     * Creates a chart.
     * 
     * @param dataset  the data for the chart.
     * 
     * @return a chart.
     */
    private JFreeChart createChart(final XYDataset dataset) {
        
        // create the chart...
        final JFreeChart chart = ChartFactory.createXYLineChart(
            "Line Chart Demo 6",      // chart title
            "X",                      // x axis label
            "Y",                      // y axis label
            dataset,                  // data
            PlotOrientation.VERTICAL,
            true,                     // include legend
            true,                     // tooltips
            false                     // urls
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
        chart.setBackgroundPaint(Color.white);

//        final StandardLegend legend = (StandardLegend) chart.getLegend();
  //      legend.setDisplaySeriesShapes(true);
        
        // get a reference to the plot for further customisation...
        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
    //    plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        
        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesLinesVisible(0, false);
        renderer.setSeriesShapesVisible(1, false);
        plot.setRenderer(renderer);

        // change the auto tick unit selection to integer units only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        // OPTIONAL CUSTOMISATION COMPLETED.
                
        return chart;
        
    }

    
    public static void main(final String[] args) {
        
        final LineChart demo = new LineChart("Line Chart Demo 6");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }
    public void ScanData(String filePath)
       {
            String fullFilePath = filePath;
            String line ="";
            BufferedReader loadLogFile;
            //2D Array
         intermediateData = new ArrayList<ArrayList<String>>();
             //Create file object and read it 
            try {
            loadLogFile= new BufferedReader(new FileReader (fullFilePath));
                
            
        //Initialize Row and column counter
         int row = 0;
         int col = 0;
         //Nested Loops to read file 
         
         while( (line = loadLogFile.readLine())!=null)
         {
             Scanner scanLine = new Scanner(line);
             intermediateData.add(new ArrayList<String>());
             while(scanLine.hasNext())
             {
                 intermediateData.get(row).add(scanLine.next());
                 col++;
             }
             row++;
         }
         }
         catch(FileNotFoundException e)
         {
             System.err.println("Error in reading logs: "+e);
         }
      catch(IOException e)
                {
                    System.err.println("Error in finding log file: "+e);
                }
            intermediateData.remove(0);
            intermediateData.remove(0);
        // Tesing region 
//        Iterator list = intermediateData.iterator();
//        while(list.hasNext())
//        {
//            System.out.println(list.next());
//        } 
       } 

}